# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from common.helper import Helper
from common.config import *
from urllib.parse import urlencode
from itertools import product
class Spider(object):
    def __init__(self):
        self.name = "交通工具意外险"
        self.mysql = MysqlClient()

    # 生产产品数据
    def genProductInfo(self):
        plan = {
            "days": ["7d", "1m", "3m", "6m", "9m", "1y"],
            "people_count": ["1"],
            "year": ["18-75"],
            "airplane_death": ["400000", "600000", "800000", "1000000", "1200000", "1400000", "1600000", "1800000",
                               "2000000"],
            "train_death": ["300000"],
            "ship_death": ["300000"],
            "bus_death": ["20000"]
        }
        for v in product(plan["days"], plan["people_count"], plan["year"], plan["airplane_death"], plan["train_death"],
                         plan["ship_death"], plan["bus_death"]):
            data = Helper.formatProductInfo(plan, v)
            yield data

    # 执行爬虫
    def requestJson(self):

        examples = {m: "planid1532657066254000" + str(d) for m, d in
                    zip([str(x) + "00000" for x in range(4, 22, 2)], [i for i in range(1, 10)])}
        for v in self.genProductInfo():
            if v["days"] == "7d":
                data = {
                    "startdate": "2020 / 04 / 09",
                    "enddate": "2020 / 04 / 16",
                    "items": examples[v["airplane_death"]],
                    "peoplecount": "1",
                    "planextendvalue": "",
                    "productcode": "EJQ_Z",
                }

            elif v["days"] == "1m":
                data = {
                    "startdate": "2020 / 04 / 09",
                    "enddate": "2020 / 05 / 09",
                    "items": examples[v["airplane_death"]],
                    "peoplecount": "1",
                    "planextendvalue": "",
                    "productcode": "EJQ_Z",
                }

            elif v["days"] == "3m":
                data = {
                    "startdate": "2020 / 04 / 09",
                    "enddate": "2020 / 07 / 09",
                    "items": examples[v["airplane_death"]],
                    "peoplecount": "1",
                    "planextendvalue": "",
                    "productcode": "EJQ_Z",
                }

            elif v["days"] == "6m":
                data = {
                    "startdate": "2020 / 04 / 09",
                    "enddate": "2020 / 10 / 09",
                    "items": examples[v["airplane_death"]],
                    "peoplecount": "1",
                    "planextendvalue": "",
                    "productcode": "EJQ_Z",

                }

            elif v["days"] == "9m":
                data = {
                    "startdate": "2020 / 04 / 09",
                    "enddate": "2021 / 01 / 09",
                    "items": examples[v["airplane_death"]],
                    "peoplecount": "1",
                    "planextendvalue": "",
                    "productcode": "EJQ_Z",
                }
            else:
                data = {
                    "startdate": "2020 / 04 / 09",
                    "enddate": "2021 / 04 / 08",
                    "items": examples[v["airplane_death"]],
                    "peoplecount": "1",
                    "planextendvalue": "",
                    "productcode": "EJQ_Z",
                }

            payload = urlencode(data).replace("+", "")
            # 执行请求
            r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)

            if r.status_code == 200:
                price = Helper.decimalParse(r.json()[9]["originPremium"])
                data = {
                    "name": self.name,
                    "company": "中国人民保险",
                    "price": price
                }
                data.update(v)
                Helper.mergeTable(self.name, self.mysql, data)
